<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Services\BaseAdminService;
use App\Models\BaseAdmin;

class AdminController extends CommonController{
	private $baseAdminService;
    public function __construct(Request $request){
    	
        parent::__construct($request);
        $this->baseAdminService = new BaseAdminService();
    }
    
    /**
     * 管理员列表
     */
    function index(){
    	$account = $this->request->input('account');
		$result = $this->baseAdminService->paginateInfo($account,$this->pageSize);
    	return view('admin.index',['data'=>$result]);
    }
    
    /**
     * 添加
     */
    function add(){
    	
    	if($this->request->isMethod('post')){
    		$param = $this->request->except('_token');
    		$result = $this->baseAdminService->addInfo($param);
    		
    		return response()->json($result);
    	}else{
    		return view('admin.add');
    	}
    	
    }
    
    
    /**
     * 编辑
     */
    function edit(){
    	$id = $this->request->input('id');
    	$where = [['id','=',$id]];
    	if($this->request->isMethod('post')){
    		$param = $this->request->except('id','_token');
    		$result = $this->baseAdminService->updateInfo($where,$param);
//  		 echo "<script>alert('创建成功');window.location.href='/admin/index'</script>";
    		return response()->json($result);
    	}else{
    		$result = $this->baseAdminService->findInfo($where);
    		return view('admin.edit',['info'=>$result['data']]);
    	}
    }
    
    /*
     * 删除
     */
    function delete(){
    	$id = $this->request->input('id');
    	$where = [['id','=',$id]];
    	$result = $this->baseAdminService->deleteInfo($where);
    	return response()->json($result);
    }
}
